package leetcode.f1t100;

/**
 * 实现开根号取整
 * https://leetcode.com/problems/sqrtx/
 *
 * @Author lichongxiao
 * @Date 2022/11/23 15:28
 */
public class Q69_SqrtX {
    public int mySqrt(int x) {
        if (x == 0 | x == 1) {
            return x;
        }
        int left = 0, right = x, mid;
        while ((mid = (right + left) / 2) > left) {
            if (mid == x / mid) {
                return mid;
            } else if (mid > x / mid) {
                right = mid;
            } else {
                left = mid;
            }
        }
        return mid;
    }

    public static void main(String[] args) {
        Q69_SqrtX bean = new Q69_SqrtX();
        System.out.println(bean.mySqrt(Integer.MAX_VALUE));
        System.out.println(bean.mySqrt(2147395599));


    }
}
